package com.test.springsecurity.service.impl;

import com.test.springsecurity.entity.AppUser;
import com.test.springsecurity.service.AuthService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

/**
 *
 * 该类实现AuthService接口
 *创建UsernamePasswordAuthenticationToken对象
 * 将获取的账号 密码放到token中
 * 调用authenticationManager 认证管理器 对用户名进行认证
 *
 * 
 *
 */
@Slf4j
@Service
public class AuthServiceImpl implements AuthService {
    @Resource
    private AuthenticationManager authenticationManager;
    @Override
    public boolean login( AppUser appUser) {
        //将用户的账号密码封装为token对象
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(appUser.getUsername(),appUser.getPassword());

        //调用认证管理器的authenticate方法进行认证 （调用springsecurity内置流程）
        Authentication authentication = authenticationManager.authenticate(authenticationToken);

        //判断是否登录成功
        if(authentication != null){
            log.info("登录成功");
            return true;
        }
        return false;
    }
}
